<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fit Layout</title>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
    <script src="../../../scripts/jquery.min.js"></script>
    <script src="../../../scripts/miniui/miniui.js"></script>
    <link href="../../../scripts/miniui/themes/default/miniui.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/icons.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/pure/skin.css" rel="stylesheet">
    <script src="../../../scripts/core.js"></script>

</head>
<body>
<style type="text/css">
    html, body {
        margin: 0;
        padding: 0;
        border: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
</style>
<div class="mini-toolbar" style="padding:2px;border-bottom:0;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a class="mini-button" iconCls="icon-add" onclick="add()" plain="true">新加</a>
                <a class="mini-button" iconCls="icon-edit" onclick="saveData()" plain="true">保存</a>
                <a class="mini-button" iconCls="icon-upload" onclick="uploadLogo()" plain="true">上传赛道图标</a>
                <a class="mini-button" iconCls="icon-upload" onclick="uploadName()" plain="true">上传赛道名称</a>
                <a class="mini-button" iconCls="icon-download" onclick="syn()" plain="true">同步配置</a>
                <a class="mini-button" iconCls="icon-close" onclick="delData()" plain="true">删除</a>
            </td>

        </tr>
    </table>
</div>
<!--撑满页面-->
<div class="mini-fit">

    <mini-datagrid _dataField="data111" allowCellEdit="true" allowCellSelect="true"
                   allowResize="true" allowSortColumn="false"
                   class="mini-datagrid"
                   id="datagrid1" idField="id" itemRenderer="itemRenderer"
                   multiSelect="true" pageSize="15"
                   showSortIcon="true"
                   style="width:100%;height:100%;"
                   url="/cmsapi/getAllRaceCfg">
        <columns>
            <column type="indexcolumn"></column>
            <column type="checkcolumn"></column>
            <column field="id" headerAlign="center" name="id">
                id
            </column>
            <column field="orderIndex">排序
                <input class="mini-spinner" maxValue="100000000" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="name">名称
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>
            <column field="chance">爆率
                <input class="mini-spinner" maxValue="100" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="rate" numberFormat="n0" width="180">数值放大率
                <input class="mini-spinner" maxValue="1000000000000000000000000000" minValue="1"

                       property="editor"
                       style="width:100%;"/>
            </column>
            <column field="lvLock" width="100">等级锁
                <input class="mini-spinner" maxValue="1000000" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="consumeEnergy" numberFormat="n0" width="100">体力消耗值
                <input class="mini-spinner" maxValue="1000000000" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="produce" numberFormat="n0" width="100">产出
                <input class="mini-spinner" maxValue="1000000000000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="inviteNumCfg" width="100">提现邀请人数
                <input class="mini-spinner" maxValue="100000000" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="withdrawAmount" numberFormat="n0" width="100">提现最低数量
                <input class="mini-spinner" maxValue="1000000000000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="freeNum" width="100">免费点击次数
                <input class="mini-spinner" maxValue="100000000" minValue="1" property="editor" style="width:100%;"/>
            </column>
            <column field="freeStatus" width="100">活动开关
                <input class="mini-spinner" property="editor" style="width:100%;"/>
            </column>
            <column field="startTime" width="100">开始时间
                <input class="mini-datepicker" format="yyyy-MM-dd HH:mm:ss" property="editor" showClearButton="false"
                       showOkButton="true"
                       showTime="true"
                       timeFormat="HH:mm:ss"/>
            </column>
            <column field="endTime" width="100">结束时间
                <input class="mini-datepicker" format="yyyy-MM-dd HH:mm:ss" property="editor" showClearButton="false"
                       showOkButton="true"
                       showTime="true" timeFormat="HH:mm:ss"/>
            </column>
            <column field="withdrawStatus" width="100">提现开关
                <input class="mini-spinner" property="editor" style="width:100%;"/>
            </column>
            <column field="exchange" width="100">汇率
                <input class="mini-spinner" maxValue="100000000000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="exchangeRate" width="100">汇率倍率
                <input class="mini-spinner" maxValue="100000000000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="logoUrl" width="100">logo图标

            </column>
            <column field="raceNameUrl" width="100">名称图标

            </column>
        </columns>
    </mini-datagrid>

</div>

<script type="text/javascript">

    var auth = GetParams()
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.setAjaxOptions({
        headers: {"userId": auth.userId, "token": auth.token}
    });
    grid.on("drawcell", function (e) {
        var record = e.record,
            column = e.column,
            field = e.field,
            value = e.value;
        //格式化日期
        if (field == "startTime") {
            if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");
        }
        //格式化日期
        if (field == "endTime") {
            if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");
        }


        //将性别文本替换成图片
        if (column.field == "logoUrl") {
            if (record.logoUrl == null) return;
            if (record.logoUrl == '') return;
            e.cellHtml = '<img src="' + record.logoUrl + '" alt="Smiley face">';
        }

        //将性别文本替换成图片
        if (column.field == "raceNameUrl") {
            if (record.raceNameUrl == null) return;
            if (record.raceNameUrl == '') return;
            e.cellHtml = '<img src="' + record.raceNameUrl + '" alt="Smiley face">';
        }

    });
    grid.load();


    function add() {

        mini.open({
            targetWindow: window,
            url: "AddRaceCfg.html?userId=" + auth.userId + "&token=" + auth.token,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {

                grid.reload();
            }
        });
    }


    function onDrawCell(e) {


    }

    function saveData() {
        if (grid.getSelecteds().length == 0) return;
        var row = grid.getSelecteds()[0];
        var json = mini.encode(row);
        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "/cmsapi/editRaceCfg",
            data: json,
            type: "post",
            contentType: 'application/json',
            headers: {"userId": auth.userId, "token": auth.token},
            success: function (text) {
                var resp = mini.decode(text)
                if (resp.code == 200) {
                    grid.reload();
                } else {
                    mini.alert("错误码:" + resp.code);
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }


    async function syn() {
        mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '同步中...'
        });
        const tokenResp = await fetch("/sys/getTargetSysToken", {
            method: 'POST',
            headers: {"userId": auth.userId, "token": auth.token},
            contentType: 'application/json'
        });

        const sysResp = await tokenResp.json();

        for (var i = 0; i < grid.getSelecteds().length; i++) {

            var row = grid.getSelecteds()[i];
            var json = mini.encode(row);

            const response = await fetch(sysResp.url + "/cmsapi/editRaceCfg",
                {
                    method: 'POST',
                    body: json,
                    headers: {
                        "userId": sysResp.loginResp.userId,
                        "token": sysResp.loginResp.token,
                        'Content-Type': 'application/json',
                    },
                });
            const resp = await response.json();
            console.log(resp)
        }
        mini.alert("同步成功");
        mini.unmask(document.body);
    }

    function delData() {
        if (grid.getSelecteds().length == 0) {
            alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    grid.loading("删除中，请稍后......");
                    var row = grid.getSelecteds()[0];
                    var json = mini.encode(row);
                    grid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delRaceCfg",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            console.log("-----------")
                            grid.reload();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }


    function uploadLogo() {
        if (grid.getSelecteds().length == 0) {
            mini.alert("没有选中任何数据")
            return;
        }
        var row = grid.getSelecteds()[0];
        mini.open({
            targetWindow: window,
            url: "UploadRaceLogo.html?userId=" + auth.userId + "&token=" + auth.token + "&raceId=" + row.id,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }

    function uploadName() {
        if (grid.getSelecteds().length == 0) {
            mini.alert("没有选中任何数据")
            return;
        }
        var row = grid.getSelecteds()[0];
        mini.open({
            targetWindow: window,
            url: "UploadRaceName.html?userId=" + auth.userId + "&token=" + auth.token + "&raceId=" + row.id,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }
</script>

</body>
</html>